<#include "/common/head.ftl">

<@showTitle title="${i18n.getMessage('offeringReceipt')} > ${i18n.getMessage('create_one')}">
	<a href="#" onclick="cc('student/showInfoStudent.sgt?id=${student.id}')">${i18n.getMessage('student')}</a>
</@showTitle>

<div id="error" style="display:none"></div>

<form action="receipt/addOfferingReceipt.sgt" validator="receipt/validateOfferingReceipt.sgt" onsubmit="submitFormValidation(this); return false;" onreset="$('error').hide()">
	<input type="hidden" name="studentId" value="${(student.id)!}"/>
	<input type="hidden" name="sessions" value="${(sessions)!}"/>
	<input type="hidden" name="feePerSession" value="${(feePerSession)!}"/>
	<input type="hidden" name="totalFee" value="${(tuition)!}"/>
	<input type="hidden" name="comment1" value="đóng tiền trễ"/>
	<input type="hidden" name="comment2" value="buổi"/>
	<input type="hidden" name="comment3" value="bảo lưu từ biên lai số "/>
	<input type="hidden" name="realFee" />

	<div class="additional">
		<b>USD:</b> <strong class="bold" id="amount">${amount?string('#,##0')!}</strong> <span id="newdate">(${(date)!})</span>
		<#if now?date?string != date?string>
			<a href="#" onClick="getLatest()" class="warning">${i18n.getMessage('currency.update')}</a>
		<#else>
			<span style="color: #090;">${i18n.getMessage('currency.update.already')}</span>
		</#if>
		
	</div>
	<table id="dataTable" class="w750" cellspacing="5" cellpadding="0">
		<tr>
			<td class="name w100">${i18n.getMessage('receipt.type')}:</td>
			<td>
				<select name="type" id="type" class="w200" onchange="reload()">
					<option value="one" <#if type="one">selected</#if>>One time</option>
					<option value="multi" <#if type="multi">selected</#if>>Multi-time</option>
				</select>
			</td>
		</tr>
		<tr>
			<td class="name w100"><@required/>${i18n.getMessage('receipt.date')}:</td>
			<td><input type="text" name="date" value="${now?date}" class="w200"/></td>
		</tr>
		<tr>
			<td class="name w100">${i18n.getMessage('student')}:</td>
			<td><a href="#" onclick="cc('student/showInfoStudent.sgt?id=${student.id}')" style="color:000">${(student.fullName)!}</a></td>
		</tr>
		<tr>
			<td class="name w100">${i18n.getMessage('level')}:</td>
			<td>
				<select id="levelId" name="levelId" class="w200" onchange="cc('receipt/showAddOfferingReceipt.sgt?levelId=' + this.value + '&studentId=${student.id}&type=' + $('type').value)">
					<option></option>
					<#list programs as program>
						${request.setAttribute("levels", program.levels)}
						<optgroup label="${program.name}">
							<#list levels as lev>
								<#if lev.hasOpeningOffering>
								<option value="${lev.id}" <#if lev == (level)!>selected</#if>>${lev.name}</option>
								</#if>
							</#list>
						</optgroup>
					</#list>
				</select>
			</td>
		</tr>
		<#if levelId??>
		<tr>
			<td class="name w100"><@required/>${i18n.getMessage('offering')}:</td>
			<td>
				<select id="offeringId" name="offeringId" class="w200" onchange="cc('receipt/showAddOfferingReceipt.sgt?levelId=' + $('levelId').value + '&offeringId=' + $('offeringId').value + '&studentId=${student.id}&type=' + $('type').value)">
					<option></option>
					<#list offerings as offering>	
						<option value="${offering.id}" <#if offeringId?? && offering.id == offeringId>selected</#if>>${(offering.startDate?date)!}-${(offering.endDate?date)!} # ${(offering.name)!}</option>
					</#list>
				</select>
			</td>
		</tr>
		<#if type="one">
		<tr>
			<td class="name w100">${i18n.getMessage('discountItem')}:</td>
			<td>
				<select id="discountItemId" name="discountItemId" class="w200" onchange="updateDiscount(this); invokeDiscount2(this)">
					<option percent="0" label=""></option>
					<#list keys as key>
					<optgroup label="Discount ${key}">
						<#assign items = discountItems(key)>
						<#list items as item>
							<#if item.active>
							<option value="${(item.id)!}" label="${(item.name)!}" percent="${(item.percent)!'0'}">${(item.name)!}</option>
							</#if>
						</#list>
					</optgroup>
					</#list>
					<optgroup label="Amount Discount">
						<#list amountDiscountItems as item>
							<#if item.active>
							<option value="${(item.id)!}" label="${(item.name)!}" amount="${(item.amount)!'0'}">${(item.name)!}</option>
							</#if>
						</#list>
					</optgroup>
				</select>
				<span id="dis1value"></span>
			</td>
			
			<td class="name w115">Tuition:</td>
			<td class="w115"><input type="text" name="tuition" value="${(tuition)!}" 
					class="right w080" disabled /> ${i18n.getMessage('currency.vnd')}</td>
		</tr>
		<tr>
			<td class="name w115">${i18n.getMessage('discountItem')} 2:</td>
			<td>
				<select id="discountItemId2" name="discountItemId2" class="w200" onchange="updateDiscount(this)" disabled="disabled">
					<option percent="0" label=""></option>
					<#list keys as key>
					<optgroup label="Discount ${key}">
						<#assign items = discountItems(key)>
						<#list items as item>
							<#if item.active>
							<option value="${(item.id)!}" label="${(item.name)!}" percent="${(item.percent)!'0'}">${(item.name)!}</option>
							</#if>
						</#list>
					</optgroup>
					</#list>
					<optgroup label="Amount Discount">
						<#list amountDiscountItems as item>
							<#if item.active>
							<option value="${(item.id)!}" label="${(item.name)!}" amount="${(item.amount)!'0'}">${(item.name)!}</option>
							</#if>
						</#list>
					</optgroup>
				</select>
				<span id="dis2value"></span>
				<input type="hidden" name="discount1" />
			</td>	
			
			<td class="name w115">${i18n.getMessage('discount')}:</td>
			<td class="w115"><input type="text" name="discount" disabled class="right w080"/> ${i18n.getMessage('currency.vnd')}</td>		
		</tr>
		<tr>
			<td class="name w100">${i18n.getMessage('maintenance')}:</td>
			<td>
				<select id="maintenanceId" name="maintenanceId" class="w200" onchange="updateMaintenance(this)">
					<option></option>
					<#list maintenances as item>
						<option value="${(item.id)!}" amount="${(item.fee)!'0'}">${(item.number)!} # ${(item.date)!}</option>
					</#list>
				</select>
			</td>
			
			<td class="name w115">${i18n.getMessage('maintenance')} ${i18n.getMessage('fee')}:</td>
			<td class="w115"><input type="text" name="maintenanceFee" disabled class="right w080"/> ${i18n.getMessage('currency.vnd')}</td>	
		</tr>
		<tr>
			<td class="name w100">${i18n.getMessage('lateSession')}:</td>
			<td><input type="text" name="lateSession" maxlength="1" class="right 170" onblur="updateLate(this)" onkeyup="data_change(this)"/></td>
			
			<td class="name w115">${i18n.getMessage('receipt.lateDiscount')}:</td>
			<td class="w115"><input type="text" name="lateDiscount" disabled class="right w080"/> ${i18n.getMessage('currency.vnd')} </td>
		</tr>
		</#if>
		<#if offeringId??>
		<tr>
			<#if type="multi">
				<td class="name w100">${i18n.getMessage('receipt.expiredDate')}:</td>
				<td><input type="text" name="expiredDate" value="${(expiredDate)?date}" style="text-align:right;width:170px"/></td>
			<#else>
				<td></td>
				<td></td>
			</#if>
			
			<td class="name w080">${i18n.getMessage('fee')}:</td>
			<#if type=="multi">
			<td><input type="text" name="fee" value="${(submittedFee)!}" class="right w080"/> ${i18n.getMessage('currency.vnd')}</td>
			<#else>
			<td><input type="text" name="fee" value="${(tuition)!}" class="right w080"/> ${i18n.getMessage('currency.vnd')}</td>
			</#if>
		</tr>
		</#if>
		<tr>
		</tr>
		<tr>
			<td class="name w100"><@required/>${i18n.getMessage('cashier')}:</td>
			<td>
				<select name="cashierId" class="w200">
					<option></option>
					<#list cashiers as cashier>	
						<option value="${cashier.id}" <#if cashier == action.USER()>selected</#if>>${(cashier.fullName)!}</option>
					</#list>
				</select>
			</td>
		</tr>
		<tr>
			<td class="name w100" style="vertical-align:top">${i18n.getMessage('comment')}</td>
			<td colspan="3"><textarea name="comment" class="w300 h075"></textarea></td>
		</tr>
		<tr>
			<td class="name w100"></td>
			<td>
				<input type="submit" value="${i18n.getMessage('add')}" class="button_style"/> 
				<input type="reset" value="${i18n.getMessage('reset')}" class="button_style"/>
			</td>
		</tr>
		</#if>
	</table>
</form>

<script>
updateFee = function(obj) {
	var tuition = obj.form.tuition;
	var discount = obj.form.discount;
	var maintenanceFee = obj.form.maintenanceFee;
	var lateDiscount = obj.form.lateDiscount;
	var type = obj.form.type;
	var total = obj.form.totalFee;
	
	var newValue = tuition.value - discount.value - maintenanceFee.value - lateDiscount.value;
	var realValue = round((type.value=="multi" ? newValue / 2 : newValue), 3);
	realValue = (realValue > 0 ? realValue : 0);
	totalValue = round(newValue, 3);	
	total.value = (parseInt(totalValue) > 0 ? totalValue : 0);
	
	var fee = obj.form.fee;
	fee.value = realValue;
	
	obj.form.realFee.value = round(tuition.value - discount.value - lateDiscount.value, 3);
}

updateDiscount = function(obj) {
	// TODO LATE
	var lateSession = obj.form.lateSession;
	lateSession.value = "";
	
	var lateDiscount = obj.form.lateDiscount;
	lateDiscount.value = "";
	
	// CHECK TUITION
	var tuition = obj.form.tuition;
	if(tuition.value == "") return;

	// CALC DISCOUNT
	var index = obj.selectedIndex;
	var discount = obj.form.discount;
	var discount1 = obj.form.discountItemId;
	var discount2 = obj.form.discountItemId2;
	var dis1, dis2;
	
	if(discount1.options[discount1.selectedIndex].getAttribute('percent') != null)
		dis1 = round(parseInt(tuition.value) * discount1.options[discount1.selectedIndex].getAttribute('percent'), 2);
	else dis1 = parseInt(discount1.options[discount1.selectedIndex].getAttribute('amount'));
	
	if(discount2.options[discount2.selectedIndex].getAttribute('percent') != null)
		dis2 = round(parseInt(tuition.value) * discount2.options[discount2.selectedIndex].getAttribute('percent'), 2);
	else dis2 = parseInt(discount2.options[discount2.selectedIndex].getAttribute('amount'));
		
	discount.value = (dis1 + dis2);
	
	if (dis1 != 0) $('dis1value').innerHTML = dis1;
	else $('dis1value').innerHTML = "";
	if (dis2 != 0) $('dis2value').innerHTML = dis2;
	else $('dis2value').innerHTML = "";
	
	updateFee(obj);
}

updateMaintenance = function(obj) {
	// TODO LATE
	var lateSession = obj.form.lateSession;
	//lateSession.value = "";
	
	var lateDiscount = obj.form.lateDiscount;
	//lateDiscount.value = "";
	
	// CHECK TUITION
	var tuition = obj.form.tuition;
	if(tuition.value == "") return;

	// CALC MAINTENANCE
	var index = obj.selectedIndex;
	var maintenanceFee = obj.form.maintenanceFee;
	maintenanceFee.value = obj.options[index].getAttribute('amount');
	
	// UPDATE FEE & COMMENT
	updateFee(obj);
}

updateLate = function(obj) {
	// TODO
	var discountItemId = obj.form.discountItemId;
	//discountItemId.selectedIndex = 0;
	
	var discount = obj.form.discount;
	//discount.value = "";
	
	var maintenanceId = obj.form.maintenanceId;
	//maintenanceId.selectedIndex = 0;
	
	var maintenanceFee = obj.form.maintenanceFee;
	//maintenanceFee.value = "";
	
	// CHECK TUITION
	var tuition = obj.form.tuition;
	if(tuition.value == "") return;
	
	// CALC LATE DISCOUNT
	updateLateDiscount(obj);
	
	// UPDATE FEE & COMMENT
	updateFee(obj);
	updateLateComment(obj);
}

updateLateDiscount = function(obj) {
	var tuition = obj.form.tuition;
	var discount = obj.form.discount;
	var maintenanceFee = obj.form.maintenanceFee;
	var retain = tuition.value - discount.value - maintenanceFee.value;
	
	var sessions = obj.form.sessions;
	var feePerSession = retain / sessions.value;
	
	var lateDiscount = obj.form.lateDiscount;
	
	if(obj.value != "")
		lateDiscount.value = parseInt(feePerSession) *  parseInt(obj.value);;
}

updateLateComment = function(obj) {
	var lateSession = obj.form.lateSession;
	var comment1 = obj.form.comment1;
	var comment2 = obj.form.comment2;
	var realComment = "";
	
	if(lateSession.value == 0)
		realComment = "";
	else 
		realComment = comment1.value + " " + lateSession.value + " " + comment2.value;
		
	var comment = obj.form.comment;
	comment.value = realComment;
}

data_change = function(field) {
    var value = field.value;
	for(var i=0;i < field.value.length; ++i) {
		var new_key = value.charAt(i);
        if(((new_key < "0") || (new_key > "9")) && !(new_key == "")) {
			field.value = "";
            break;
		}
	}
}

reload = function() {
	var levelId = $('levelId').value;
	var offeringId = $('offeringId').value;
	var type = $('type').value;
	
	cc('receipt/showAddOfferingReceipt.sgt?levelId=' + levelId + '&studentId=${student.id}&offeringId=' + offeringId + '&type=' + type)
}
invokeDiscount2 = function(selection) {
	var value = selection.options[selection.selectedIndex].getAttribute('percent');
	if (value != "0") selection.form.discountItemId2.disabled = false; 
	else {
		selection.form.discountItemId2.selectedIndex = 0;
		selection.form.discountItemId2.disabled = true;
		updateDiscount(selection.form.discountItemId2);
	}
}

getLatest = function() {
	new Ajax.Request('system/getLatest.sgt', {
  		onSuccess: function(transport) {
  			text = transport.responseText.split(";");
    		$('amount').innerHTML = text[0];
    		$('newdate').innerHTML = "("+text[1]+")";
  		}
	});
}
</script>